<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page session="false" %>
<%@ page isELIgnored ="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<script type="text/javascript">
//<![CDATA[
(function(){
	DataView={
		addNew:function(){
			$ku.loadDojoExt({url:"../../inspector/${entity.id}/dataviewnew",target:"tab_dataview",title:KppModule.commonResource.create,closable:true,
				onShow:function(){
					dijit.byId("sc_inspector_toolbar").selectChild("tb_inspector_new",true);
				}});
			dijit.byId("tab_dataview").resize();
		},
		callEdit:function(){
			if(dataviewGrid.selection.getSelected().length==0 || (!dataviewGrid.selection.getSelected()[0]))return;
			var id = dataviewGrid.selection.getSelected()[0].i.id;
			$ku.loadDojoExt({url:"../../inspector/${entity.id}/dataviewnew?dataid="+id,target:"tab_dataview",title:KppModule.commonResource.edit,closable:true,
				onShow:function(){
					dijit.byId("sc_inspector_toolbar").selectChild("tb_inspector_edit",true);
				}});
			dijit.byId("tab_dataview").resize();
		},
		remove:function(did,button){
			var options={
					domain:Inspector._resource.dataview,
					refresh:DataView.refresh,
					url:"../../inspector/${entity.id}/datadel/"+did,
					button:dijit.byId("btn_dataview_del"),
					dialogId:"dialog_dataview_delete",
					resource:Inspector._resource
			};
			KppModule.restProxy(options).remove();
		},
		callRemove:function(){
			if(dataviewGrid.selection.getSelected().length==0 || (!dataviewGrid.selection.getSelected()[0]))return;
			var id = dataviewGrid.selection.getSelected()[0].i.id;
			var label = dataviewGrid.selection.getSelected()[0].i['${entity.labelProperty}'];
			if(id!=null && id!=''){
				var delDialog = dijit.byId("dialog_dataview_delete");
				if(!delDialog) delDialog = new dijit.Dialog({
					id:'dialog_dataview_delete',
					title:KppModule.commonResource.confirm,
					style: "padding:0px;width:300px;height:200px;background-color:#FFF;"
				});
				delDialog.set("content", "<div style=\"padding:20px;text-align:center\">"+$ksub(KppModule.commonResource.confirmDelete,[label])+"</div>" +
					"<div style=\"text-align:center\"><div data-dojo-type=\"dijit.form.Button\" data-dojo-props=\"iconClass:'dijitEditorIcon dijitEditorIconDelete', id:'btn_dataview_del',showLabel:true, busyLabel:'"+KppModule.commonResource.removing+"'\">"+KppModule.commonResource.remove+
					"<script type=\"dojo/method\" event=\"onClick\" args=\"evt\">DataView.remove('"+id+"',this)<\/script>"+
					"</div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+
					"<div data-dojo-type=\"dijit.form.Button\" data-dojo-props=\"iconClass:'dijitEditorIcon dijitEditorIconCancel', showLabel:true\">"+KppModule.commonResource.cancel +
					"<script type='dojo/method' event='onClick' args='evt'>dijit.byId('dialog_dataview_delete').hide();<\/script></div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+
					"</div>"
				);
				delDialog.show();
			}
		},
		refresh:function(removeflag){
			dijit.byId("pane_grid_container").set("href", "../../inspector/${entity.id}/dataview?removeflag="+removeflag);
		},
		closeCurrent:function(){
			var child=dijit.byId("tab_dataview").selectedChildWidget;
			$ku.destroyExtChild(dijit.byId("tab_dataview"),child);
		},
		back:function(){
			dijit.byId("tab_dataview").selectChild("grid_dataview",true);
		},
		store:new dojox.data.QueryReadStore({url:'../../inspector/${entity.id}/data?removeflag=${removeflag}'})
	};
	DataView.key="datakey";
	DataView.gridStructure=[
		{cells:[
			{field:"_item",name:"序号",formatter:KppModule.sno,width:"40px",filterable:false},
			<c:forEach items="${entity.properties}" var="p" varStatus="loopStatus">
				<c:if test="${p.defaultShow}">
					<c:choose>
						<c:when test="loopStatus.last">
							{field:'${p.name}',name:'${p.label}',formatter:$kt.escapeBlank,width:"auto",datatype:"${p.dataType}",autoComplete:true}
						</c:when>
						<c:otherwise>
							{field:'${p.name}',name:'${p.label}',formatter:$kt.escapeBlank,width:"auto",datatype:"${p.dataType}",autoComplete:true},
						</c:otherwise>
					</c:choose>
				</c:if>
			</c:forEach>
		]}
	];
   	DataView.filter={
		itemsName:'${entity.label}',
		closeFilterbarButton:true,
		isServerSide: true,
      	setupFilterQuery: function(commands, request){
	    	if(commands.filter && commands.enable){
	        	console.log(commands.filter);
	        	if(commands.filter.op=="all" || commands.filter.op=="any"){
	        		var operators = "";
	        		var parameters = "";
	        		var values = "";
	        		for(var i in commands.filter.data){
	        			if(commands.filter.data[i].op == "isEmpty") {
	        				operators += commands.filter.data[i].op;
		        			parameters += commands.filter.data[i].data[0].data;
			        	}else if(commands.filter.data[i].op=="not"){
			        		operators += commands.filter.data[i].op+commands.filter.data[i].data[0].op;
		        			parameters += commands.filter.data[i].data[0].data[0].data;
		        			values += commands.filter.data[i].data[0].data[1].data; 
			        	}else{
			        		operators += commands.filter.data[i].op;
		        			parameters += commands.filter.data[i].data[0].data;
		        			values += commands.filter.data[i].data[1].data; 
			        	}
	        			if(i < commands.filter.data.length-1) {
	        				operators += "-";
	        				parameters += "-";
	        				values += "-";
	        			}
	        		}
	        		request.query = {
	        			"operator":operators,
	        			"parameter":parameters,
	        			"value":values,
	        			"connector":commands.filter.op
	        		}
	        		/**
	        		var param = "{'connector':" + commands.filter.op;
	        		for(var i in commands.filter.data) {
	        			param += ",'operator':" + commands.filter.data[i].op;
	        			param += ",'parameter':" + commands.filter.data[i].data[0].data;
	        			param += ",'value':" + commands.filter.data[i].data[1].data;
	        		}
	        		param += "}";
	        		alert(param);
	        		request.query = param;
	        		**/
	        	}else{
	        		if(commands.filter.op == "isEmpty") {
		        		request.query = {
		        			"operator":commands.filter.op,
		        			"parameter":commands.filter.data[0].data
		        		}
		        	}else if(commands.filter.op=="not"){
		        		request.query = {
		        			"operator":commands.filter.op+commands.filter.data[0].op,
		        			"parameter":commands.filter.data[0].data[0].data,
		        			"value":commands.filter.data[0].data[1].data
		        		}
		        	}else{
		        		request.query = {
		        			"operator":commands.filter.op,
		        			"parameter":commands.filter.data[0].data,
		        			"value":commands.filter.data[1].data
	        			}
		        	}
	        	}
	      	}
		}
	};
})();
//]]>
</script>
<span style="display:none">To solve IE7/8 script event tag problem.</span>
<div id="tab_dataview" data-dojo-type="dijit/layout/TabContainer">
	<div id="grid_dataview" data-dojo-id="dataviewGrid" data-dojo-type="dojox/grid/EnhancedGrid" data-dojo-props="title:'${entity.label}',region:'center',structure:DataView.gridStructure,canSort:function(){return false;},
		store:DataView.store,plugins:{filter:DataView.filter}" >
		<script type="dojo/on" data-dojo-event="show">
			dijit.byId("sc_inspector_toolbar").selectChild("tb_inspector_view",true);
		</script>
	</div>
</div>